package com.wang.service.impl;

import com.aaa.entity.EEmpPer;
import com.aaa.entity.EmpInfo;
import com.aaa.security.util.WebUtils;
import com.aaa.util.JwtUtil;
import com.aaa.vo.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.service.EEmpInfoService;
import com.wang.mapper.EEmpInfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 *
 */
@Service
public class EEmpInfoServiceImpl extends ServiceImpl<EEmpInfoMapper, EmpInfo>
        implements EEmpInfoService {
    @Autowired
    private EEmpInfoMapper eEmpInfoService;
    @Override
    public Result updatePassword(String newPassword, String oldPassword) {
        //调用安全框架的加密器
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        //根据工具类查询车token，token有我们保存的用户信息和权限,前面我们登录的时候已经在redis里面生成token了
        String token = WebUtils.getRequest().getHeader("token");
        Map<String, Object> map = JwtUtil.getTokenChaim(token);
        String username = (String) map.get("username");
        QueryWrapper<EmpInfo> wrapper = new QueryWrapper<EmpInfo>();
        wrapper.eq("ename", username);
        EmpInfo empInfo = eEmpInfoService.selectOne(wrapper);
        boolean matches = passwordEncoder.matches(newPassword,empInfo.getPassword());
        EmpInfo empInfo1 = new EmpInfo();
        if (matches==true) {
            String encode = passwordEncoder.encode(oldPassword);
            LambdaUpdateWrapper<EmpInfo> wrapper1 = new LambdaUpdateWrapper<EmpInfo>();
            wrapper1.eq(EmpInfo::getEname, username);
            wrapper1.set(EmpInfo::getPassword, encode);
            eEmpInfoService.update(empInfo1, wrapper1);
            return new Result(2000, "汪东正");
        }
        return new Result(5000, "error");
    }

    public static void main(String[] args) {
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();

        System.out.println(passwordEncoder.matches("123456", "$2a$10$IoHoAPyhjJtGx3LGrzpzgujtDbMu432CybidE2uWAngSthAvqrvP."));

    }
}





